import { ESCesiumViewer } from "@czmSrc/ESCesiumViewer";
import { MultiPointsAppendEditing } from ".";
import { ESJVector3D, geoDestination, geoDistance, geoHeading } from "earthsdk3";
import { extendClassProps, react, reactArray, reactArrayWithUndefined, ReactivePropsToNativePropsAndChanged } from "xbsj-base";

export class DoublePointsAppendEditing extends MultiPointsAppendEditing {
    public init() {
        // 初始化
        const origin = this.startPosition;
        if (!origin) {
            return;
        }
        const distance = this.distance;
        const rotation = this.rotation;
        const groundDistance = distance * Math.cos(rotation[1] * Math.PI / 180);
        const des = geoDestination(origin, groundDistance, rotation[0]);
        if (!des) {
            this.positions = [origin];
            return;
        }
        const height = distance * Math.sin(rotation[1] * Math.PI / 180);
        des[2] += height;
        this.positions = [origin, des];
        if (this.editingPoints.length == 2) {
            this.editingPoints.get(0).position = origin;
            this.editingPoints.get(1).position = des;
        }
    }
    constructor(czmViewer: ESCesiumViewer) {
        super(czmViewer);
        this.loop = true; //默认不闭合
        this.maxPointsNum = 2;
        {
            const update = () => {
                if (!this.positions || this.positions.length == 0) {
                    this.distance = 1;
                    this.rotation = [90, 0, 0];
                    this.startPosition = [0, 0, 0];
                    return;
                }
                if (this.positions.length == 1) {
                    this.distance = 1;
                    this.rotation = [90, 0, 0];
                    return;
                };
                this.startPosition = this.positions[0];
                this.endPosition = this.positions[1];
                const heading = geoHeading(this.startPosition, this.endPosition);
                const groundDistance = geoDistance(this.startPosition, this.endPosition);
                if (groundDistance <= 0) {
                    this.rotation = [90, 0, 0];
                    this.distance = 1;
                    return;
                }
                const height = this.endPosition[2] - this.startPosition[2];
                const pitch = Math.atan(height / groundDistance) * 180 / Math.PI;
                const distance = Math.sqrt(groundDistance * groundDistance + height * height);
                this.rotation = [heading, pitch, 0];
                this.distance = distance;
            }
            update();
            this.ad(this.positionsChanged.don(update));
        }
    }
}
export namespace DoublePointsAppendEditing {
    export const createDefaultProps = () => ({
        ...MultiPointsAppendEditing.createDefaultProps(),
        startPosition: reactArrayWithUndefined<ESJVector3D>(undefined),
        endPosition: reactArrayWithUndefined<ESJVector3D>(undefined),
        rotation: reactArray<ESJVector3D>([90, 0, 0]),
        distance: react(1),
    })
}
extendClassProps(DoublePointsAppendEditing.prototype, DoublePointsAppendEditing.createDefaultProps);
export interface DoublePointsAppendEditing extends ReactivePropsToNativePropsAndChanged<ReturnType<typeof DoublePointsAppendEditing.createDefaultProps>> { }